(function (jQuery){
	
	jQuery.net={};

	jQuery.hostname=jQuery.hostname||location.hostname;
	jQuery.net.cache=gw.cache[jQuery.hostname]=gw.cache[jQuery.hostname]||{'useInfos':{},'tidInfos':{}};	
	jQuery.net.config={
		'sendURL':'http://bbs.2500sz.com/bbs/forum.php?mod=post&action=reply&fid={#fid}&tid={#tid}&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1',
		'lockURLT':'http://bbs.2500sz.com/bbs/forum.php?mod=redirect&tid={#tid}&goto=lastpost',
		'sendDate':'&subject=&message=',
		'lockT':10000,
		'lookT':1000,
		'lookfl':60,
		'sendfl':20,
		'_reloadT':1500,
		'_nextT':6000,
		'ckManage':gw.ckManage(["bbs.2500sz.com"]),
		'getfl':function(t){ 
					var flsRe=/<em>(\d+)<\/em>/g,flRe=/\d+/; 
					var fls=t.match(flsRe);
					return parseInt(fls[fls.length-1].match(flRe)[0]);
				}
	}

	//jQuery.isEmptyObject
	jQuery.net.expand={
		'_default':{},
		'__init':function(obj){
			jQuery.extend(obj,this._default);
			for(var i in obj){
				if(jQuery.isFunction(obj[i])){
					obj[i]=obj[i]();
				}
			}
			return this;
		},
		'_set':function(obj){
			for(var i in obj){
				var value=obj[i];
				jQuery('[name='+i+'],#'+i).each(function(){
					this.value=value;
				});
			}
		},
		'_toString': function(obj){
			var html="";
			for(var i in obj){
				html+="&"+i+"="+obj[i];
			}
			return html.substr(1);
		}
	}

	jQuery.net.reqInfos={};

	jQuery.net.useInfo={
		'__init':function(opt){
			var re=/[^\x00-\xff]/;
			if(!!!opt.name){
				opt.name=opt.userId;
			}
			opt.userId2=re.test(opt.userId)?gw.convert(opt.userId):opt.userId;
			return this;
		},	
		'_send':function(useInfo,tid){
			var tidInfo=jQuery.net.cache.tidInfos[tid];
			var l=tidInfo.mess.length;
			var req=jQuery.net.reqInfos[useInfo.userId]['req'];
			jQuery.extend(useInfo,{'tid':tid,'mess':tidInfo.mess[tidInfo.j++%l],'num':1});
			req.addEventListener("load", function(){
					var reqInfo=jQuery.net.reqInfos[useInfo.userId],l=tidInfo.mess.length;
					if (this.readyState == 4) { 		
						if(reqInfo['time']!=0){
							clearTimeout(reqInfo['time']),reqInfo['time']=0;
						}

						if(tidInfo.ch){
							useInfo['mess']=tidInfo.mess[tidInfo.j++%l];
						}
						useInfo['num']++;
						if(tidInfo.num==0||useInfo['num']<=tidInfo.num){
							if(useInfo['num']==tidInfo.num){
								this.removeEventListener("load",arguments.callee,true);
							}
							if(tid==useInfo['tid']&&tidInfo['send']=='send'){
								setTimeout(jQuery.net.useInfo._post,jQuery.net.config._nextT,useInfo);	
								return ;
							}
						}
						if(tidInfo['send']=="end"){
							this.removeEventListener("load",arguments.callee,true);
						}
					}
				}, true);	
				
			this._post(useInfo);
		},
		'_post':function (useInfo){
			var req=jQuery.net.reqInfos[useInfo.userId]['req'],tidInfo=jQuery.net.cache.tidInfos[useInfo['tid']];
			req.open('POST', tidInfo.url, true); 
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.setRequestHeader('wgu',jQuery.net.config.ckManage.toString(useInfo['cookie']));
			req.send(jQuery.net.useInfo.expandString(useInfo)+jQuery.net.config.sendDate+useInfo['mess']);
			jQuery.net.reqInfos[useInfo.userId]['time']=setTimeout(jQuery.net.useInfo._reload,jQuery.net.config._reloadT,useInfo);		
		},
		'_reload':function (useInfo){
			var reqInfo=jQuery.net.reqInfos[useInfo.userId];
			reqInfo['time']=0;
			if(reqInfo['req'].readyState<3){
				jQuery.net.useInfo._post(useInfo);
			}
		},
		'expandString':function (useInfo){
			return jQuery.net.expand._toString(useInfo['expand']);
		}
	}

	jQuery.net.panel={
		'_add':function(opt){
			
			if(jQuery.isArray(opt)){
				var re={'userId':opt[0],'passWord':opt[1]};
				if(opt.length==3){
					re.name=opt[2];
				}
				opt=re;
			}
			jQuery.net.useInfo.__init(opt)._init(opt);
			jQuery.net.cache.useInfos[opt.userId]=opt;
			return this;
		},
		'_panel':function (x,button){
			var html=[],t1='<option value="{#i}">{#name}</option>';
			var html2=[],t2='<span ><input type="checkbox" checked name="useInfo" value="{#i}">{#name}</span>';
			var useInfos=jQuery.net.cache.useInfos,x=x||"";
			for(var i in useInfos){
				if(useInfos[i].name==x){
					html.unshift(t1.format({'i':i,'name':useInfos[i].name}));
				}else{
					html.push(t1.format({'i':i,'name':useInfos[i].name}));
				}
				html2.push(t2.format({'i':i,'name':useInfos[i].name}));
				jQuery.net.reqInfos[useInfos[i].userId]={'time':0,'req':new XMLHttpRequest()};
			}

			if(jQuery("#wgu_tool").length==0){
				jQuery('<div id="wgu_tool" style="z-index:99999;bottom:0px;border-width: 1px;position: fixed;border-style: solid;border-radius: 11px 11px 0px 0px;padding:5px;background-color:White;">'
					+'<div><select id="wgu_sel" >'+html.join('')+'</select></div>'
					+'<div>'+html2.join('')+'</div>'
					+'<div>welcome to my tools! welcome to my tools! </div>'
					+'</div>').appendTo('body');
			}else{
				jQuery("#wgu_tool").html('<div><select id="wgu_sel" >'+html.join('')+'</select></div>'
					+'<div>'+html2.join('')+'</div>'
					+'<div>welcome to my tools! welcome to my tools! </div>');
			}
			jQuery('#wgu_sel').bind('change',function (){
					var i=this.value;
					jQuery.net.cache['current']=i;
					jQuery.net.config.ckManage.remove().set(useInfos[i]['cookie']);
					if(!!useInfos[i].expand){
						jQuery.net.expand._set(useInfos[i].expand);
					}
				});
				
				
			var temp=jQuery('<div>'
				+'num:<input value="0" id="wgu_num" style="width:30px">'
				+'floor:<input id="wgu_fl" value="3000"/>'
				+'<input type="checkbox" name="wgu_ch" id="wgu_ch">change'
				+'<input type="checkbox" name="wgu_lk" id="wgu_lk">lock'
				+'</div>').appendTo('#wgu_tool');
			button.forEach(function(v,i){
				jQuery('<button>'+v.name+'</button>').appendTo(temp).bind('click',v);
			});
		
			return this;
		},
		'_send':function(fid,tid,message){
			var config=jQuery.net.config;
			var tidInfo=jQuery.net.cache.tidInfos[tid]={'fid':fid,'tid':tid,'mess':message,'fl':jQuery('#wgu_fl').val().split(','),'send':'send'};
			var url=config.sendURL.format(tidInfo);
			gw.ob.add('request',url,'get','wgu','Cookie');
			gw.ob.add('request',url,'set','wgu',null);
			
			var num=jQuery('#wgu_num').val()*1||0;
			jQuery.extend(tidInfo,{'url':url,'num':num,'ch':jQuery('#wgu_ch')[0].checked,'j':0,'isLock':jQuery('#wgu_lk')[0].checked});
			
			if(num==0){
				this._lock(tid)();
			}
			document.getElementsByName('useInfo').forEach(function(v){
				if(v.checked){
					jQuery(v).parent()[0].style.color='olivedrab';
					jQuery.net.useInfo._send(jQuery.net.cache.useInfos[v.value],tid);
				}
			})
			return this;
		},
		'_lock':function (tid,main,t){
			if(typeof main=="undefined"){
				main=true;
			}
			var config=jQuery.net.config;
			var tidInfo=jQuery.net.cache.tidInfos[tid];
			var t=t||config.lockT,url=config.lockURLT.format(tidInfo);
			var req=new XMLHttpRequest();
			var looking=false;
			req.addEventListener("load", function(){
					var that=jQuery.net.panel;
					if (this.readyState == 4) { 
						var fl=parseInt(config.getfl(req.responseText));
						var flag=false;
						if(tidInfo.isLock==true){
							tidInfo['fl'].some(function(v){
								v=parseInt(v);
								if(fl<v-config.lookfl){
									flag=true;
									if(looking==true){
										looking=false;
										that._post(tid);
									}
									tidInfo['send']='send';
								}else if(v-config.lookfl<fl&&fl<v-config.sendfl&&v-config.lookfl>0){
									flag=true;
									tidInfo['send']='waite';
									t=config.lookT;
									if(looking==false){
										looking=true;
										for(var i=0;i<5;i++){
											that._lock(tid,false,t)();
										}
									}
								}else if(v-config.sendfl<=fl&&fl<=v&&v-config.lookfl>0){
									flag=true;
									if(looking==true){
										looking=false;
										that._post(tid);
									}
									t=config.lockT;
									tidInfo['send']='send';
								}
								return flag;
							})
						}else{
							tidInfo['fl'].some(function(v){
								v=parseInt(v);
								if(fl<=v+20){
									flag=true;
								}
								return flag;
							})
						}
						if(flag==false){
							tidInfo['send']='end';looking=false;
							var use=document.getElementsByName('useInfo'),useInfos=jQuery.net.cache.useInfos;
							for(var i in useInfos){
								if(useInfos[i]['tid']==tid){
									jQuery('[value='+i+']',use).parent()[0].style.color='#000000';
								}
							}
							this.removeEventListener("load",arguments.callee,true);
						}else{
							if(main==false){
								looking==true&&setTimeout(_p,t);
							}else{
								setTimeout(_p,t);
							}
						}
					}
				}, true);
			function _p(){
				req.open('GET',url+'&wgu='+new Date().valueOf(), true); 
				req.send(null);
			}
			return _p;
		},
		'_post':function(tid){
			var reqInfos=jQuery.net.reqInfos,useInfos=jQuery.net.cache.useInfos,tidInfos=jQuery.net.cache.tidInfos;
			for(var i in reqInfos){
				if(tid==reqInfos[i]['tid']&&tidInfos[tid]['send']=='send'){
					jQuery.net.useInfo._post(useInfos[i]);
				}
			}
		},
		'_toAry':function(){
			var useInfos=jQuery.net.cache.useInfos,r=[];
			for(var i in useInfos){
				r.push(useInfos[i]['userId']);
			}
			jQuery.net.cache.userIds=r;
			return this;
		}
	}
})(___jQuery)